#include "audio_sources/ogg_source.h"

#include "source_descriptor.h"

using namespace pita;

OggSource::OggSource(const SourceDescriptor& sd) :
	finished_ (false)
{
	if (ov_fopen(const_cast<char*>(sd.url().c_str()), &vfile_))
		finished_ = true;
}

OggSource::~OggSource()
{
	ov_clear(&vfile_);
}

bool OggSource::finished() const
{
	return finished_;
}

bool OggSource::nextChunk(DataChunk& chunk,
						  unsigned int timeout)
{
	int current_section;

	long ret = ov_read(&vfile_,
					   &chunk[0],
					   chunk.size(),
					   0,2,1,
					   &current_section);

	if (ret < 0) return false;

	chunk.resize(ret);

	if (ret == 0)
		finished_ = true;
	
	return true;
}
